package com.hula.onion.book.server.spider.platforms.eastlending;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/30 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class EastLendingSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(5);

        map.put("loginName", form.getUserName());
        map.put("password", form.getPassword());
        map.put("captcha", form.getTicket());
        map.put("backurl", "");
        map.put("backhost", "");

        return map;

    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseAccountBalance(String html) {
        if(StringUtils.isEmpty(html)){
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
        try{
            AccountInfo accountInfo = new AccountInfo();
            boolean success = JSONObject.parseObject(html).getBoolean("success");
                if (success) {
                    JSONObject data = JSONObject.parseObject(html).getJSONObject("data");

                    //投资总额
                    double amount = data.getDouble("investAmountTotal");
                    //累计收益
                    double investInterest = data.getDouble("investInterest");
                    //待收收益
                    double expectInterest = data.getDouble("expectInterest");
                    accountInfo.setAmount(new BigDecimal(amount));
                    accountInfo.setTotalIntrest(new BigDecimal(investInterest));
                    accountInfo.setWaitIntrest(new BigDecimal(expectInterest));

                    return new CommonResponse(accountInfo);
                }
                return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);

        }catch (Exception e){
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }


}
